<template>
	<view>
		<!-- 单据列表 -->
		<web-view ref="webview" :src="webUrl" v-if="webUrl" @message="message"></web-view>
		<!-- PDA扫码 -->
		<m-pda-scancode v-if="$utils.isPda()" listenName="WmsListScan"
			:SetContent="{action:'android.intent.action.SCANRESULT',extra:'value'}"></m-pda-scancode>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				deviceInfo: {}, // 设备信息
				webUrl: '', // 单据列表路径
				menu: {} // 菜单
			}
		},

		created() {
			let that = this;
			that.menu = that.$store.state.Menu;
			that.webUrl = that.menu.AP_Link;
		},

		// 导航返回、安卓返回、navigateback
		onBackPress(options) {
			// 安卓设备需要主动查询,iOS不需要
			if (this.deviceInfo.osName == 'android') {
				this.refreshList();
			}
		},

		onLoad() {
			let that = this;
			// 监听PDA扫码事件
			uni.$on('WmsListScan', function(content) {
				that.SetEvalJsFun('SetScanData', content);
			})
			uni.$on('WmsListChoice', function(content) {
				that.SetEvalJsFun('SetChoiceData', JSON.stringify(content));
			})

			that.refreshList();


			// 监听下拉筛选选中的返回
			uni.$on('Wms_Choice', function(data) {
				console.log(data)
				that.SetEvalJsFun('GetBaseCreenBack', JSON.stringify(data));

			})
		},

		onUnload() {
			// 销毁事件监听 防止重复返回
			uni.$off('WmsListChoice');
			uni.$off('WmsListScan');
		},

		methods: {

			// 刷新单据列表数据
			refreshList() {
				var that = this;
				setTimeout(() => {
					that.deviceInfo = JSON.parse(uni.getStorageSync('sysDeviceInfo'));
					// 获取当前账套IP地址与协议地址
					if (that.Request.serverIP()) {
						let TheApi = {
							serverIP: uni.getStorageSync('serverIP'),
							IPAgreement: that.Request.IPAgreement(),
							safeTop: that.deviceInfo.safeAreaInsets.top
						}
						that.SetEvalJsFun('SetServiceIP', JSON.stringify(TheApi));
					}
					that.SetEvalJsFun('SetTitleName', JSON.stringify(that.menu));
				}, 500)
			},

			/** SetEvalJsFun 调用html内页面方法
			 * @param {String} FunName 调用html页面内的方法名
			 * @param {*} data 传入的参数
			 */
			SetEvalJsFun(FunName, data) {
				var that = this;
				try {
					let currentWebview = that.$scope.$getAppWebview().children()[0];

					currentWebview.evalJS(`vm.${FunName}('${data}')`)
				} catch (e) {
					//TODO handle the exception
				}
			},

			// 列表跳转详情处理
			navigateHandler(obj) {
				// console.log(obj);
				uni.navigateTo({
					url: '/wms/edit?id=' + obj.data.FStockID
				})
			},

			// 自带摄像头扫码处理
			scanHandler() {
				let that = this;
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['qrCode'],
					success: function(res) {
						// 返还扫码结果
						that.SetEvalJsFun('SetScanData', res.result);
					}
				})
			},

			// 下拉选择处理
			baseHandler(obj) {
				let url = '/commonPage/Tool/baseScreen';

				this.$store.commit('ChangeAndSetStateData', {
					Name1: 'BaseScreenParm',
					data: obj.data
				})

				uni.navigateTo({
					url: url
				})
			},

			// webview消息监听
			message(event) {
				// console.log(event);
				let obj = event.detail.data[0];
				let action = obj.action;

				switch (action) {
					case 'openCamera':
						this.scanHandler();
						break;
					case 'baseScreen':
						this.baseHandler(obj);
						break;
					case 'add':
						this.navigateHandler(obj);
						break;
					case 'edit':
						this.navigateHandler(obj);
						break;
					default:
						break;
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>